home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / dejagnu.lha / dejagnu-1.0.1 / tcl / compat / strtol.c < prev    next >
C/C++ Source or Header  |  1992-11-06  |  2KB  |  89 lines

  1. /* 
  2.  * strtol.c --
  3.  *
  4.  *    Source code for the "strtol" library procedure.
  5.  *
  6.  * Copyright 1988 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/lib/tcl/compat/RCS/strtol.c,v 1.1 91/09/22 15:42:49 ouster Exp $ SPRITE (Berkeley)";
  18. #endif /* not lint */
  19.  
  20. #include <ctype.h>
  21.  
  22.  
  23. /*
  24.  *----------------------------------------------------------------------
  25.  *
  26.  * strtol --
  27.  *
  28.  *    Convert an ASCII string into an integer.
  29.  *
  30.  * Results:
  31.  *    The return value is the integer equivalent of string.  If endPtr
  32.  *    is non-NULL, then *endPtr is filled in with the character
  33.  *    after the last one that was part of the integer.  If string
  34.  *    doesn't contain a valid integer value, then zero is returned
  35.  *    and *endPtr is set to string.
  36.  *
  37.  * Side effects:
  38.  *    None.
  39.  *
  40.  *----------------------------------------------------------------------
  41.  */
  42.  
  43. long int
  44. strtol(string, endPtr, base)
  45.     char *string;        /* String of ASCII digits, possibly
  46.                  * preceded by white space.  For bases
  47.                  * greater than 10, either lower- or
  48.                  * upper-case digits may be used.
  49.                  */
  50.     char **endPtr;        /* Where to store address of terminating
  51.                  * character, or NULL. */
  52.     int base;            /* Base for conversion.  Must be less
  53.                  * than 37.  If 0, then the base is chosen
  54.                  * from the leading characters of string:
  55.                  * "0x" means hex, "0" means octal, anything
  56.                  * else means decimal.
  57.                  */
  58. {
  59.     register char *p;
  60.     int result;
  61.  
  62.     /*
  63.      * Skip any leading blanks.
  64.      */
  65.  
  66.     p = string;
  67.     while (isspace(*p)) {
  68.     p += 1;
  69.     }
  70.  
  71.     /*
  72.      * Check for a sign.
  73.      */
  74.  
  75.     if (*p == '-') {
  76.     p += 1;
  77.     result = -(strtoul(p, endPtr, base));
  78.     } else {
  79.     if (*p == '+') {
  80.         p += 1;
  81.     }
  82.     result = strtoul(p, endPtr, base);
  83.     }
  84.     if ((result == 0) && (endPtr != 0) && (*endPtr == p)) {
  85.     *endPtr = string;
  86.     }
  87.     return result;
  88. }
  89.